package com.nbsaas.boot.site.rest.resource;

import com.nbsaas.boot.site.api.apis.DynamicPageCatalogApi;
import com.nbsaas.boot.site.data.entity.DynamicPageCatalog;
import com.nbsaas.boot.site.api.domain.request.DynamicPageCatalogRequest;
import com.nbsaas.boot.site.api.domain.response.DynamicPageCatalogResponse;
import com.nbsaas.boot.site.api.domain.simple.DynamicPageCatalogSimple;
import com.nbsaas.boot.site.rest.convert.DynamicPageCatalogSimpleConvert;
import com.nbsaas.boot.site.rest.convert.DynamicPageCatalogEntityConvert;
import com.nbsaas.boot.site.rest.convert.DynamicPageCatalogResponseConvert;
import com.nbsaas.boot.site.data.repository.DynamicPageCatalogRepository;

import java.io.Serializable;
import com.nbsaas.boot.jpa.data.core.BaseResource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import org.springframework.data.jpa.repository.support.JpaRepositoryImplementation;

import java.util.function.Function;
import com.nbsaas.boot.rest.request.PageRequest;
import com.nbsaas.boot.rest.response.ListResponse;
import com.nbsaas.boot.site.api.domain.request.DynamicPageCatalogSearch;
import com.nbsaas.boot.rest.utils.TreeUtils;
import java.util.List;
/**
*   页面分类-业务接口实现
*/
@Transactional
@Service
public class DynamicPageCatalogResource extends BaseResource<DynamicPageCatalog,DynamicPageCatalogResponse, DynamicPageCatalogSimple, DynamicPageCatalogRequest>  implements DynamicPageCatalogApi {

    @Resource
    private DynamicPageCatalogRepository dynamicPageCatalogRepository;

    @Override
    public JpaRepositoryImplementation<DynamicPageCatalog, Serializable> getJpaRepository() {
        return dynamicPageCatalogRepository;
    }

    @Override
    public Function<DynamicPageCatalog, DynamicPageCatalogSimple> getConvertSimple() {
        return new DynamicPageCatalogSimpleConvert();
    }

    @Override
    public Function<DynamicPageCatalogRequest, DynamicPageCatalog> getConvertForm() {
        return new DynamicPageCatalogEntityConvert();
    }

    @Override
    public Function<DynamicPageCatalog, DynamicPageCatalogResponse> getConvertResponse() {
        return new DynamicPageCatalogResponseConvert();
    }



    @Transactional(readOnly = true)
    @Override
    public ListResponse<DynamicPageCatalogSimple> list(PageRequest request) {
        DynamicPageCatalogSimpleConvert convert=new DynamicPageCatalogSimpleConvert();
        if (request instanceof DynamicPageCatalogSearch){
             DynamicPageCatalogSearch searchRequest=(DynamicPageCatalogSearch)request;
             convert.setFetch(searchRequest.getFetch());
        }
        return listSimple(request,convert);
    }


    @Transactional(readOnly = true)
    @Override
    public ListResponse<DynamicPageCatalogSimple> root(DynamicPageCatalogSearch search) {
        ListResponse<DynamicPageCatalogSimple> result=new  ListResponse<>();
        List<DynamicPageCatalogSimple> simples = list(search).getData();
        result.setData(TreeUtils.tree(simples));
        return result;
    }


}


